<template>
    <div>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card class="box-card" shadow="never">
        <template #header>
          <div class="card-header">
            <img src="../../assets/logo.svg" style="width: 50px;height: 50px;">
            <span>用户列表</span>
          </div>
        </template>
        <el-row :gutter="10">
          <el-col :span="4">
<!--            <el-input v-model="input4" :prefix-icon="Search">-->

<!--            </el-input>-->
            <el-input placeholder="请输入用户名称" v-model="searchContent" :prefix-icon="Search"></el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button type="primary" @click="addUserInfo()">添加</el-button>
          </el-col>
        </el-row>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="编号" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.ID }}</span>
            </template>
          </el-table-column>
          <el-table-column label="名称" width="180">
            <template #default="scope">
              <el-popover effect="light" trigger="hover" placement="top">
                <template #reference>
                  <div class="name-wrapper">
                    <el-tag size="medium">{{ scope.row.user_name }}</el-tag>
                  </div>
                </template>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="年龄" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.age }}</span>
            </template>
          </el-table-column>
          <el-table-column label="性别" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.sex }}</span>
            </template>
          </el-table-column>
          <el-table-column label="phone" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.phone }}</span>
            </template>
          </el-table-column>
          <el-table-column label="email" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.email }}</span>
            </template>
          </el-table-column>
          <el-table-column label="照骗" style="width: 50px;height: 50px">
            <template #default="scope">
              <el-popover effect="light" trigger="hover" placement="top">
                <template #reference>
                  <div class="name-wrapper">
                    <img :src="url + scope.row.img"/>
                    <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fqqpublic.qpic.cn%2Fqq_public%2F0%2F0-2603408470-836402D991765E5F073F24AC480F5C3F%2F0%3Ffmt%3Djpg%26size%3D44%26h%3D829%26w%3D588%26ppv%3D1.jpg&refer=http%3A%2F%2Fqqpublic.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642507356&t=3df6ef9a14832d733877e3f5f4e39e98"
                         style="width: 180px;height: 180px"/>
                  </div>
                </template>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="户号" width="180">
            <template #default="scope">
              <el-icon><timer /></el-icon>
              <span style="margin-left: 10px">{{ scope.row.family_id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="primary" :icon="Edit" size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
              >
              <el-button
                  size="mini"
                  type="danger" :icon="Delete"
                  @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            v-model:currentPage="currentPage"
            :page-sizes="[3, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="useSizeChange"
            @current-change="useCurrentChange"
        >
        </el-pagination>
      </el-card>
    </div>
  <AddUser :dialogVisible="dialogVisible" @onCloseDialog="closeDialogVisible"></AddUser>
  <EditUser :editDialogVisible="editDialogVisible" :uId="uId" @onCloseEditDialog="closeEditDialogVisible" v-if="editDialogVisible === true "></EditUser>
</template>

<script>
import {reactive, onMounted, toRefs} from "vue";
import AddUser from "../components/AddUser.vue";
import EditUser from  "../components/EditUser.vue"
import {
  Search,
  Edit,
  Delete,
} from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import axios from "axios";
function loaData(state){
  const params = {
    limit:state.pageSize,
    page:state.currentPage,
    search_content:state.searchContent
  }
  axios.get("/api/users",{params}).then((res) => {
    console.log(res)
    state.tableData = res.data.data.res.data
    state.total = res.data.data.res.total
  })
}
//监听pageSize改变的事件
function useHandleSizeChange(state){
  const useSizeChange = (pageSize) => {
    console.log(pageSize)
    state.pageSize = pageSize
    loaData(state)
  }
  return {
    useSizeChange
  }
}
//监听当前页面切换的事件
function useHandleCurrentPage(state){
  const useCurrentChange = (page) => {
    console.log(page)
    state.currentPage = page
    loaData(state)
  }
  return {
    useCurrentChange,
  }
}
//实现用户搜索功能
function useHandleSearch(state){
  const handleSearch = () => {
    state.currentPage = 1
    const params = {
      limit:state.pageSize,
      page:state.currentPage,
      search_content:state.searchContent
    }
    axios.get("/api/users", {params}).then((res) => {
      state.tableData = res.data.data.res.data
      state.total = res.data.data.res.total
    })
  }
  return {
    handleSearch
  }
}
//编辑用户信息
function useEdit(state){
  const handleEdit = (index, row) => {
    state.editDialogVisible = true;
    state.uId = row.ID
  }
  return {
    handleEdit
  }
}
//删除用户
function useDelete(state){
  const handleDelete = (index, row) => {
    ElMessageBox.confirm("你确定要删除该记录吗？", "提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      callback:(active)=>{
        if (active === 'confirm'){
          axios.delete("/api/user",{params:{ID:row.ID}}).then((res) => {
            if (res.data.code === 20000){
              ElMessage({
                message: '删除成功',
                type: 'success',
              })
              loaData(state)
            }else {
              ElMessage({
                message: '删除失败',
                type: 'error',
              })
            }
          })
        }
      }
    })
  }
  return {
    handleDelete,
  }
}
export default {
  components: {
    AddUser,
    EditUser
  },
  setup() {
    const state = reactive({
      tableData: [],
      dialogVisible: false, //控制新增弹窗的开关
      editDialogVisible: false, //控制编辑弹窗的开关
      total:0,
      currentPage:1,
      pageSize:3,
      searchContent:"",
      uId:0
    })
    const url = import.meta.env.VITE_APP_URL
    onMounted(() => {
      loaData(state)
    });
    const addUserInfo = () => {
      state.dialogVisible=true
    };
    //关闭添加用户
    const closeDialogVisible = (visible, res) => {
      state.dialogVisible = visible
      if (res != undefined){
        loaData(state)
      }
    }
    //关闭编辑用户
    const closeEditDialogVisible = (visible) => {
      loaData(state)
      state.editDialogVisible = visible;
    }
    return {
      ...toRefs(state),
      url,
      addUserInfo,
      closeDialogVisible,
      Delete,
      Edit,
      Search,
      ...useHandleSizeChange(state),
      ...useHandleCurrentPage(state),
      ...useHandleSearch(state),
      ...useEdit(state),
      closeEditDialogVisible,
      ...useDelete(state)
    }
  }
}
</script>

<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
</style>